package com.example.demo.controller;

import com.example.demo.model.Music;
import com.example.demo.model.User;
import com.example.demo.service.LoveMusicService;
import com.example.demo.tools.Constant;
import com.example.demo.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Autowired
    private LoveMusicService loveMusicService;



    // 收藏歌曲
    @Transactional
    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam Integer musicid, HttpServletRequest request) {
        // 获取用户id
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        Integer userid = user.getId();

        // 验证 歌曲是否已经收藏
        Music music = loveMusicService.findLoveMusicByMusicIdAndUserId(userid, musicid);

        // 歌曲已经收藏
        if (music != null) {
            return new ResponseBodyMessage<>(-1,"歌曲已收藏 请出现选择", false);
        }

        // 走到这 说明歌曲还没收藏
        if (loveMusicService.insertLoveMusic(userid, musicid)) {
            return new ResponseBodyMessage<>(0,"歌曲收藏成功", true);
        }
        return new ResponseBodyMessage<>(-1,"歌曲收藏失败", false);
    }




    // 查看自己收藏的歌曲
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String title, HttpServletRequest request) {
        // 获取用户id
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        Integer user_id = user.getId();


        List<Music> list = null;
        if (title == null) {
            list = loveMusicService.findLoveMusicByKeyAndUserId(user_id);
        }else {
            list = loveMusicService.findLoveMusicByKeyAndUserId(title,user_id);
        }
        return new ResponseBodyMessage<>(0,"查询结果为： ",list);
    }



    // 取消收藏
    @RequestMapping("/deletelovemusic")
    @Transactional
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam Integer music_id, HttpServletRequest request) {
        // 获取用户id
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        Integer user_id = user.getId();

        // 取消收藏
        Integer ret = loveMusicService.deleteLoveMusic(user_id, music_id);
        if (ret != 0) {
            return new ResponseBodyMessage<>(0,"取消收藏成功",true);
        }
        return new ResponseBodyMessage<>(-1,"取消收藏失败",false);
    }
}
